package org.carshared.principal;

import java.text.DecimalFormat;
import java.util.ArrayList;

import org.carshared.R;
import org.carshared.sqlite.DBAdapterDateTravel;
import org.carshared.sqlite.DBAdapterPassenger;
import org.carshared.utils.PassengerBeen;
import org.carshared.utils.TravelBeen;

import com.google.gson.Gson;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class InformActivity extends Activity {

	private DBAdapterPassenger dbHelperPassenger;
	private DBAdapterDateTravel dbHelperDateTravel;
	private ArrayList<PassengerBeen> p;
	private ArrayList<String> d;
	private TravelBeen t;
	Gson gson;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inform);
		dbHelperPassenger = new DBAdapterPassenger(this);
		dbHelperDateTravel = new DBAdapterDateTravel(this);

		Bundle bundle = getIntent().getExtras();
		gson = new Gson();
		String travel = bundle.getString("dataTravel");
		t = gson.fromJson(travel, TravelBeen.class);

		TextView nameTravel = (TextView) findViewById(R.id.nameTravel);
		nameTravel.setText(t.getOrigenDestino());

		p = dbHelperPassenger.fetchAllPassenger(t.getOrigenDestino());

		setDatasPassenger();
	}

	private void setDatasPassenger() {
		// TODO Auto-generated method stub
		String name, name2, name3, name4;
		TextView namePassenger1, namePassenger2, namePassenger3, namePassenger4;
		TextView dateTravelPassenger, dateTravelPassenger2, dateTravelPassenger3, dateTravelPassenger4;
		TextView acumulado1, acumulado2, acumulado3, acumulado4;
		DecimalFormat df = new DecimalFormat("#.##");
		switch (t.getnPasajeros()) {
		case 0:
		case 1:
			// Pasajero 1
			name = p.get(0).getNombrePasajero();
			namePassenger1 = (TextView) findViewById(R.id.namePassenger1);
			namePassenger1.setText(name);
			dateTravelPassenger = (TextView) findViewById(R.id.dateTravel1);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name);
			if (d.size() > 0) {
				dateTravelPassenger.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado1 = (TextView) findViewById(R.id.acumulado1);
				acumulado1.setText(df.format(p.get(0).getAcumulado()));
			}

			break;
		case 2:
			// Pasajero 1
			name = p.get(0).getNombrePasajero();
			namePassenger1 = (TextView) findViewById(R.id.namePassenger1);
			namePassenger1.setText(name);
			dateTravelPassenger = (TextView) findViewById(R.id.dateTravel1);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name);
			if (d.size() > 0) {
				dateTravelPassenger.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado1 = (TextView) findViewById(R.id.acumulado1);
				acumulado1.setText(df.format(p.get(0).getAcumulado()));
			}
			// Pasajero 2
			name2 = p.get(1).getNombrePasajero();
			namePassenger2 = (TextView) findViewById(R.id.namePassenger2);
			namePassenger2.setText(name2);
			dateTravelPassenger2 = (TextView) findViewById(R.id.dateTravel2);
			if (d.size() > 0) {
				d = dbHelperDateTravel.fetchDateTravelBeen(
						t.getOrigenDestino(), name2);
				dateTravelPassenger2.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado2 = (TextView) findViewById(R.id.acumulado2);
				acumulado2.setText(df.format(p.get(1).getAcumulado()));
			}

			break;
		case 3:
			// Pasajero 1
			name = p.get(0).getNombrePasajero();
			namePassenger1 = (TextView) findViewById(R.id.namePassenger1);
			namePassenger1.setText(name);
			dateTravelPassenger = (TextView) findViewById(R.id.dateTravel1);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name);
			if (d.size() > 0) {
				dateTravelPassenger.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado1 = (TextView) findViewById(R.id.acumulado1);
				acumulado1.setText(df.format(p.get(0).getAcumulado()));
			}
			// Pasajero 2
			name2 = p.get(1).getNombrePasajero();
			namePassenger2 = (TextView) findViewById(R.id.namePassenger2);
			namePassenger2.setText(name2);
			dateTravelPassenger2 = (TextView) findViewById(R.id.dateTravel2);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name2);
			if (d.size() > 0) {
				dateTravelPassenger2.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado2 = (TextView) findViewById(R.id.acumulado2);
				acumulado2.setText(df.format(p.get(1).getAcumulado()));
			}
			// Pasajero 3
			name3 = p.get(2).getNombrePasajero();
			namePassenger3 = (TextView) findViewById(R.id.namePassenger3);
			namePassenger3.setText(name3);
			dateTravelPassenger3 = (TextView) findViewById(R.id.dateTravel3);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name3);
			if (d.size() > 0) {
				dateTravelPassenger3.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado3 = (TextView) findViewById(R.id.acumulado3);
				acumulado3.setText(df.format(p.get(2).getAcumulado()));
			}
			break;
		default:

			// Pasajero 1
			name = p.get(0).getNombrePasajero();
			namePassenger1 = (TextView) findViewById(R.id.namePassenger1);
			namePassenger1.setText(name);
			dateTravelPassenger = (TextView) findViewById(R.id.dateTravel1);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name);
			if (d.size() > 0) {
				dateTravelPassenger.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado1 = (TextView) findViewById(R.id.acumulado1);
				acumulado1.setText(df.format(p.get(0).getAcumulado()));
			}
			// Pasajero 2
			name2 = p.get(1).getNombrePasajero();
			namePassenger2 = (TextView) findViewById(R.id.namePassenger2);
			namePassenger2.setText(name2);
			dateTravelPassenger2 = (TextView) findViewById(R.id.dateTravel2);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name2);
			if (d.size() > 0) {
				dateTravelPassenger2.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado2 = (TextView) findViewById(R.id.acumulado2);
				acumulado2.setText(df.format(p.get(1).getAcumulado()));
			}

			// Pasajero 3
			name3 = p.get(2).getNombrePasajero();
			namePassenger3 = (TextView) findViewById(R.id.namePassenger3);
			namePassenger3.setText(name3);
			dateTravelPassenger3 = (TextView) findViewById(R.id.dateTravel3);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name3);
			if (d.size() > 0) {
				dateTravelPassenger3.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado3 = (TextView) findViewById(R.id.acumulado3);
				acumulado3.setText(df.format(p.get(2).getAcumulado()));
			}
			// Pasajero 4
			name4 = p.get(3).getNombrePasajero();
			namePassenger4 = (TextView) findViewById(R.id.namePassenger4);
			namePassenger4.setText(name4);
			dateTravelPassenger4 = (TextView) findViewById(R.id.dateTravel4);
			d = dbHelperDateTravel.fetchDateTravelBeen(t.getOrigenDestino(),
					name4);
			if (d.size() > 0) {
				dateTravelPassenger4.setText(d.get(0) + "/"
						+ d.get(d.size() - 1));
				acumulado4 = (TextView) findViewById(R.id.acumulado4);
				acumulado4.setText(df.format(p.get(3).getAcumulado()));
			}
			break;
		}

	}
}
